using System;
using System.Web;
using System.Web.Security;
using CodeStory.Core;
using CodeStory.Core.Authentication;
using CodeStory.Core.UI;

namespace CodeStory.WebApplication.Modules.Core
{
	[NavigationPortlet("CodeStory_Core_Login")]
	public partial class Login : Portlet
	{
		protected override void OnInit(EventArgs e) {
			btnLogin.Click += new EventHandler(ButtonLogin_Click);
			lnkLoginOff.Click += new EventHandler(LinkButtonLoginOff_Click);
		}

		void LinkButtonLoginOff_Click(object sender, EventArgs e) {
			CodeStoryAuthenticationManager.LoginOff();
		}

		private void SetActiveView() {
			if (UserContext.Current.IsAuthenticated) {
				view.ActiveViewIndex = 1;
				lblLoginStatus.Text = string.Format("Welcome to CodeStory!{0}",UserContext.Current.UserName);
			} else {
				view.ActiveViewIndex = 0;
			}
		}

		void ButtonLogin_Click(object sender, EventArgs e) {
			string userName = txtUserName.Text;
			string password = txtPassword.Text;
			CodeStoryAuthenticationManager.ValidateUser(userName,password);
		}

		protected override void OnPreRender(EventArgs e) {
			SetActiveView();
			base.OnPreRender(e);
		}
	}
}